package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Contact;
import cn.gson.springboot.model.service.ContactService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ContactController {
    @Autowired
    ContactService contactService;

    @RequestMapping("/contact-all-list")
    @ResponseBody
    public Map<String,Object> selectAllContact(Integer page, Integer size){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contact> con = contactService.selectAllContact();
        for (Contact c:con){
            System.out.println(c);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",con);
        return map;
    }

    @RequestMapping("/contact-type-list")
    @ResponseBody
    public Map<String,Object> selectContactByContactType(Integer page, Integer size,Integer contactType){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contact> con = contactService.selectContactByContactType(contactType);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",con);
        return map;
    }

    @RequestMapping("/contact-search-list")
    @ResponseBody
    public Map<String,Object> selectContactBySearchValue(Integer page, Integer size,String searchValue){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Contact> con = contactService.selectContactBySearchValue(searchValue);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",con);
        return map;
    }


    @RequestMapping("/contact-list")
    @ResponseBody
    public List<Contact> contactList(Integer customerId){
        List<Contact> contacts = contactService.selectContactByCustomer(customerId);
        return contacts;
    }

    @RequestMapping("/contact-name-list")
    @ResponseBody
    public List<Contact> selectContactByCustomerName(String customerName){
        List<Contact> contacts = contactService.selectContactByCustomerName(customerName);
        return contacts;
    }
    @RequestMapping("/contact-buy-name")
    @ResponseBody
    public List<Contact> selectContactByContactName(String contactName){
        List<Contact> contacts = contactService.selectContactByContactName(contactName);
        return contacts;
    }

    @RequestMapping("/add-contact")
    @ResponseBody
    public void addNewContact(@RequestBody Contact contact){
        System.out.println(contact);
        contactService.addNewContact(contact);
    }
}
